package com.cms.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.cms.constant.CmsConstant;
import com.cms.dao.mapper.LotteryTimeMapper;
import com.cms.exception.SystemDBException;
import com.cms.model.TrAdminUser;
import com.cms.model.TrLotteryTime;
import com.cms.model.TrLotteryVo;
import com.cms.service.LotteryTimeService;

@Service
public class LotteryTimeServiceImpl implements LotteryTimeService {

	@Autowired
	private LotteryTimeMapper lotteryTimeMapper;
	 
	@Override
	public void updateLotteryTime(TrLotteryTime time) throws SystemDBException 
	{
		this.lotteryTimeMapper.updateLotteryTime(time);
	}

	@Override
	public void initLotteryTimeData(TrLotteryVo lt) throws SystemDBException
	{
		int result = this.lotteryTimeMapper.queryLotteryTime(lt.getId());
		if(result<=0)
		{
			HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
			TrAdminUser tu=(TrAdminUser) request.getSession().getAttribute(CmsConstant.CURRENT_USER);
			int num = lt.getLotteryNum();
			List<TrLotteryTime> times = new ArrayList<TrLotteryTime>();
			TrLotteryTime time =null;
			for(int i=1;i<=num;i++)
			{
				time = new TrLotteryTime();
				time.setLotteryId(lt.getId()); 
				time.setLotteryPeriods(i); 
				time.setCreateUser(tu.getId());
				time.setModifyUser(tu.getId());
				times.add(time);
			}
			this.lotteryTimeMapper.saveLotteryTimes(times);
		}
	} 

}
